package gprc.mydemo;

import grpc.mydemo.*;
import io.grpc.stub.StreamObserver;

public class StudentServiceImpl extends StudentServiceGrpc.StudentServiceImplBase {

    @Override
    public void getRealNameByUsername(MeRequest request, StreamObserver<MyResponse> responseObserver) {
        System.out.println("接受到客户端:" + request.getUsername());
        //设置返回值
        responseObserver.onNext(MyResponse.newBuilder().setRealname("张三").build());
        //标识该方法结束
        responseObserver.onCompleted();
    }

    @Override
    public void getStudentByAge(StudentRequest request, StreamObserver<StudentResponse> responseObserver) {
        System.out.println("接受到客户端:" + request.getAge());
        //设置流式返回值
        responseObserver.onNext(StudentResponse.newBuilder().setAge(18).setCity("上海").setName("老八").build());
        responseObserver.onNext(StudentResponse.newBuilder().setAge(17).setCity("上海").setName("老七").build());
        responseObserver.onNext(StudentResponse.newBuilder().setAge(16).setCity("上海").setName("老六").build());
        responseObserver.onNext(StudentResponse.newBuilder().setAge(15).setCity("上海").setName("老五").build());
        //标识该方法结束
        responseObserver.onCompleted();
    }

    @Override
    public StreamObserver<StudentRequest> getStudentsWrapperByAges(StreamObserver<StudentResponseList> responseObserver) {

        return new StreamObserver<StudentRequest>() {
            @Override
            public void onNext(StudentRequest studentRequest) {
                System.out.println("接受客户端消息：" + studentRequest.getAge());
            }

            @Override
            public void onError(Throwable throwable) {
                System.err.println(throwable.getMessage());
            }

            @Override
            public void onCompleted() {
                StudentResponse build1 = StudentResponse.newBuilder().setName("小万").setAge(18).setCity("湖北").build();
                StudentResponse build2 = StudentResponse.newBuilder().setName("小白").setAge(17).setCity("湖北").build();

                responseObserver.onNext(StudentResponseList.newBuilder().addStudetnResponse(build1).addStudetnResponse(build2).build());
                responseObserver.onCompleted();
            }
        };
    }

    @Override
    public StreamObserver<StreamRquest> biTalk(StreamObserver<StreamResponse> responseObserver) {
        return new StreamObserver<StreamRquest>() {
            @Override
            public void onNext(StreamRquest streamRquest) {
                System.out.println("客户端消息："+streamRquest.getRquesetInfo());
                responseObserver.onNext(StreamResponse.newBuilder().setResponseInfo("服务器接受到的消息："+streamRquest.getRquesetInfo()).build());
            }

            @Override
            public void onError(Throwable throwable) {
                System.err.println(throwable.getMessage());
            }

            @Override
            public void onCompleted() {
                responseObserver.onCompleted();
            }
        };
    }
}
